/**
 * Yuliy Khorolinskyy
 * 2012
 *
 * Reaktionsspiel
 * gui
 * HighscorePanel.java
 *
 */
package gui.menu;

import gui.DialogCollection;
import gui.GUI;
import gui.MenuButton;

import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import javax.swing.ButtonGroup;
import javax.swing.border.LineBorder;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT;

import control.HighscoreMenuManager;

import data.HighScores;
import data.HighscoreEntry.DIFFICULTY;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JScrollPane;

/**
 * @author y.khorolinskyy
 *
 */
public class HighscoreMenu extends JPanel {
	
	private JTable table;
	private HighScores hs;
	private final ButtonGroup difficultyButtonGroup = new ButtonGroup();
	private final JRadioButton[] difficultyButtons = new JRadioButton[3];
	private DIFFICULTY currentlyDisplayedDifficulty = DEFAULT_DIFFICULTY_TO_DISPLAY_FIRST;
	private JButton btnDeleteHighscores;
	
	public static final DIFFICULTY DEFAULT_DIFFICULTY_TO_DISPLAY_FIRST = DIFFICULTY.EASY;
	private final HighscoreMenuManager manager;
	private JScrollPane scrollPane;


	/**
	 * Create the panel.
	 */
	public HighscoreMenu(HighScores hs, HighscoreMenuManager manager) {
		
		this.hs = hs;
		this.manager = manager;
		
		setSize(new Dimension(450, 375));
		setPreferredSize(new Dimension(450, 375));
		setMinimumSize(new Dimension(450, 375));
		setLayout(new BorderLayout(0, 0));
		
		JPanel captionPanel = new JPanel();
		captionPanel.setBackground(GUI.BGMENU);
		add(captionPanel, BorderLayout.NORTH);
		captionPanel.setLayout(new GridLayout(0, 1, 0, 0));
		
		JLabel lblHighscores = new JLabel("Highscores");
		lblHighscores.setFont(new Font("Comic Sans MS", Font.BOLD, 24));
		lblHighscores.setHorizontalAlignment(SwingConstants.CENTER);
		lblHighscores.setBackground(GUI.BGMENU);
		lblHighscores.setForeground(GUI.FGMENU);
		captionPanel.add(lblHighscores);
		
		JPanel difficoultyPannel = new JPanel();
		difficoultyPannel.setBackground(GUI.BGMENU);
		captionPanel.add(difficoultyPannel);
		
		JRadioButton rdbtnEasy = new JRadioButton("EASY");
		rdbtnEasy.setForeground(GUI.FGMENU);
		rdbtnEasy.setBackground(GUI.BGMENU);
		difficultyButtons[0] = rdbtnEasy;
		difficultyButtonGroup.add(rdbtnEasy);
		rdbtnEasy.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		rdbtnEasy.setToolTipText("Select this option to view all highscores reached while playing on \"EASY\"");
		rdbtnEasy.addActionListener(manager);
		difficoultyPannel.add(rdbtnEasy);
		
		JRadioButton rdbtnMedium = new JRadioButton("MEDIUM");
		rdbtnMedium.setForeground(GUI.FGMENU);
		rdbtnMedium.setBackground(GUI.BGMENU);
		difficultyButtons[1] = rdbtnMedium;
		difficultyButtonGroup.add(rdbtnMedium);
		rdbtnMedium.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		rdbtnMedium.setToolTipText("Select this option to view all highscores reached while playing on \"MEDIUM\"");
		rdbtnMedium.addActionListener(manager);
		difficoultyPannel.add(rdbtnMedium);
		
		JRadioButton rdbtnHard = new JRadioButton("HARD");
		rdbtnHard.setForeground(GUI.FGMENU);
		rdbtnHard.setBackground(GUI.BGMENU);
		difficultyButtons[2] = rdbtnHard;
		difficultyButtonGroup.add(rdbtnHard);
		rdbtnHard.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		rdbtnHard.setToolTipText("Select this option to view all highscores reached while playing on \"HARD\"");
		rdbtnHard.addActionListener(manager);
		difficoultyPannel.add(rdbtnHard);
		
		JPanel scorePanel = new JPanel();
		scorePanel.setBackground(GUI.BGMENU);
		scorePanel.setForeground(GUI.FGMENU);
		add(scorePanel, BorderLayout.CENTER);
		
		scrollPane = new JScrollPane();
		scrollPane.setForeground(GUI.BGMENU);
		scrollPane.setBorder(null);
		scrollPane.setViewportBorder(null);
		scrollPane.setMinimumSize(new Dimension(20, 20));
		scrollPane.setFocusTraversalKeysEnabled(false);
		scrollPane.setFocusable(false);
		scorePanel.add(scrollPane);
		
		JPanel bottomButtonPanel = new JPanel();
		add(bottomButtonPanel, BorderLayout.SOUTH);
		bottomButtonPanel.setBackground(GUI.BGMENU);
		
		JButton btnBack = new MenuButton("BACK",manager);
		btnBack.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		bottomButtonPanel.add(btnBack);
		
		btnDeleteHighscores = new MenuButton("DELETE HIGHSCORES",manager);
		btnDeleteHighscores.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		bottomButtonPanel.add(btnDeleteHighscores);
		selectDifficulty(DEFAULT_DIFFICULTY_TO_DISPLAY_FIRST);
	}
	
	public void selectDifficulty(DIFFICULTY d) {
		difficultyButtonGroup.setSelected(difficultyButtons[d.ordinal()].getModel(), true);
		
		table = new JTable(hs.getHsAsSwingTableModel(d)){
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.setShowVerticalLines(false);
		table.setShowGrid(false);
		table.setOpaque(false);
		adoptNewTable();
		scrollPane.setViewportView(table);
		
		currentlyDisplayedDifficulty = d;

	}
	
	private void adoptNewTable() {

		table.setMinimumSize(new Dimension(200, 150));
		table.setBorder(null);
		table.setRowHeight(25);
		table.setRequestFocusEnabled(false);
//		table.setIntercellSpacing(new Dimension(10, 30));
		table.setFocusTraversalKeysEnabled(false);
		table.setFocusable(false);
		table.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setFont(new Font("Comic Sans MS", Font.PLAIN, 14));
		table.setRowSelectionAllowed(false);
		table.setBackground(GUI.BGMENU);
		table.setForeground(Color.WHITE);
		table.setOpaque(false);
		table.setShowVerticalLines(false);
		

	}
	
	public DIFFICULTY getDifficulty(){
		return currentlyDisplayedDifficulty;
	}
	
}
